package com.shangxian.giveithome.service.impl;

import com.shangxian.giveithome.code.CommonCode;
import com.shangxian.giveithome.entity.PageableDetails;
import com.shangxian.giveithome.entity.UserDetails;
import com.shangxian.giveithome.entity.community.IssueImg;
import com.shangxian.giveithome.entity.report.BlackList;
import com.shangxian.giveithome.entity.report.InputBlackList.ReportList;
import com.shangxian.giveithome.exception.ExceptionUtils;
import com.shangxian.giveithome.mapper.IssueImgMapper;
import com.shangxian.giveithome.mapper.ReportMapper;
import com.shangxian.giveithome.service.ReportService;
import com.shangxian.giveithome.utils.PageUtils;
import com.shangxian.giveithome.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * 举报中心业务层
 */
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private IssueImgMapper issueImgMapper;



    /**
     * 举报用户可以一直举报同一个人吗？还是要加个次数控制
     * 禁封用户还要显示他发布的宠物信息吗
     * 该用户已经被禁，应该不可以举报了吧
     * 根据多条举报，确实后全部设置为禁封！
     * 查询该举报用户是否已经被禁用，禁用用户状态仍然为1
     * @param reportList
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void submitReport(ReportList reportList) {
        UserDetails userDetails = redisUtils.getHeader(request);
        BlackList blackList = new BlackList();
        blackList.setImgId(reportList.getImgId());
        blackList.setOtherContent(reportList.getOtherContent());
        blackList.setReportContent(reportList.getReportContent());
        blackList.setUserId(userDetails.getId());
        blackList.setReportTime(new Date());
        blackList.setName(reportList.getName());
        blackList.setPhone(reportList.getPhone());
        blackList.setType(reportList.getType());
        blackList.setWechat(reportList.getWechat());
//        blackList.setReportId(reportList.getReportId());
        int i = reportMapper.submitReport(blackList);
        if (i<1){
            ExceptionUtils.error(CommonCode.PARAMETER_ERROR);
        }
    }

    @Override
    public PageableDetails findBlackListByPhone(long id, Pageable pageable) {
        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        int total = reportMapper.findBlackListTotalByPhone(id);
        if (total ==0){
            return new PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),new ArrayList<>());
        }
        List<BlackList> blackLists = reportMapper.findBlackListByPhone(id, pageNumber * pageSize, pageSize);
        return new PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),blackLists);
    }

    @Override
    public PageableDetails findBlackListByWechat(String wechat, Pageable pageable) {
        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        int total = reportMapper.findBlackListTotalByWechat(wechat);
        if (total ==0){
            return new PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),new ArrayList<>());
        }
        List<BlackList> blackLists = reportMapper.findBlackListByWechat(wechat, pageNumber * pageSize, pageSize);
        return new PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),blackLists);
    }

    @Override
    public BlackList findBlackListDetails(long id) {
        BlackList blackListDetails = reportMapper.findBlackListDetails(id);
        if (blackListDetails == null){
            return new BlackList();
        }
        return blackListDetails;
    }

    @Override
    public PageableDetails backFindBlackListUserA(Pageable pageable) {
        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        int total = reportMapper.backFindBlackListUserTotalA();
        if (total == 0){
            return new PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),new ArrayList<>());
        }
        List<BlackList> blackLists = reportMapper.backFindBlackListUserA(pageNumber * pageSize, pageSize);
        return new PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),blackLists);

    }

    @Override
    public PageableDetails backFindBlackListUserB(Pageable pageable) {
        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        int total = reportMapper.backFindBlackListUserTotalB();
        if (total == 0){
            return new PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),new ArrayList<>());
        }
        List<BlackList> blackLists = reportMapper.backFindBlackListUserB(pageNumber * pageSize, pageSize);
        return new PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),blackLists);

    }

    @Override
    public PageableDetails backFindBlackListUserC(Pageable pageable) {
        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        int total = reportMapper.backFindBlackListUserTotalC();
        if (total == 0){
            return new PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),new ArrayList<>());
        }
        List<BlackList> blackLists = reportMapper.backFindBlackListUserC(pageNumber * pageSize, pageSize);
        return new PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),blackLists);

    }

    @Override
    public PageableDetails backFindBlackListDetailsListA(long id, Pageable pageable) {
        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        int total = reportMapper.backFindBlackListDetailsListTotalA(id);
        if (total == 0){
            return new PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),new ArrayList<>());
        }
        List<BlackList> blackLists = reportMapper.backFindBlackListDetailsListA(id,pageNumber*pageSize,pageSize);
        return new PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),blackLists);
    }

    @Override
    public PageableDetails backFindBlackListDetailsListB(long id, Pageable pageable) {
        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        int total = reportMapper.backFindBlackListDetailsListTotalB(id);
        if (total == 0){
            return new PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),new ArrayList<>());
        }
        List<BlackList> blackLists = reportMapper.backFindBlackListDetailsListB(id,pageNumber*pageSize,pageSize);
        return new PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),blackLists);

    }

    @Override
    public PageableDetails backFindBlackListDetailsListC(long id, Pageable pageable) {
        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        int total = reportMapper.backFindBlackListDetailsListTotalC(id);
        if (total == 0){
            return new PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),new ArrayList<>());
        }
        List<BlackList> blackLists = reportMapper.backFindBlackListDetailsListC(id,pageNumber*pageSize,pageSize);
        return new PageableDetails(pageNumber,pageSize,total,returnPages(total,pageSize),blackLists);

    }

    @Override
    public BlackList backFindBlackListUserDetails(long id) {
        BlackList blackList = reportMapper.backFindBlackListUserDetails(id);
        if (blackList == null){
            return  new BlackList();
        }
        if (blackList.getImgId() != null){
            List<IssueImg> img = issueImgMapper.findIssueImgById(blackList.getImgId());
            blackList.setImg(img);
            System.out.println(blackList);
            return blackList;
        }
        return blackList;
    }

    @Override
    public void backDisableUser(BlackList blackList) {
        blackList.setAuditTime(new Date());
        int count = reportMapper.backDisableUser(blackList);
        if (count<1){
            ExceptionUtils.error(CommonCode.DATA_ERROR);
        }
    }

    @Override
    public void backNormalUser(BlackList blackList) {
        int count = reportMapper.backNormalUser(blackList);
        blackList.setAuditTime(new Date());
        if (count<1){
            ExceptionUtils.error(CommonCode.DATA_ERROR);
        }
    }


    /**
     * 返回
     *
     * @param pageNumber 当前页
     * @param pageSize   每页条数
     * @param count      总条数
     * @param list       返回数据
     * @param <T>        泛型
     * @return
     */
    public <T> PageableDetails returnPageable(int pageNumber, Integer pageSize, Integer count, List<T> list) {
        return new PageableDetails<>(pageNumber, pageSize, count, list);
    }


    /**
     * 返回总页数
     *
     * @param total
     * @param pageSize
     * @return
     */
    public int returnPages(int total, int pageSize) {
        return PageUtils.returnPages(total, pageSize);
    }

}
